package com.timguan.render.service.common;

import com.timguan.render.dataprocessor.processor.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by guankaiqiang on 2014/12/20.
 */
public class B2CMallServiceConfig {
    private static final Logger logger = LoggerFactory.getLogger(B2CMallServiceConfig.class);
    private static final String PAGE_NOT_FOUND = "com.timguan.render.pageNotFound";
    //不同环境cdn回源地址不一样
    private static final String CDN_DOMAIN = "com.timguan.render.cdndomain";
    public static final String PC_BASE_RES = "com.timguan.render.pcres.base";
    public static final String H5_BASE_RES = "com.timguan.render.h5res.base";
    private String pageNotFoundSite;
    private String cdnDomain;
    //pc 基础库访问前缀
    private String pcBaseResource;
    //h5 基础库访问前缀
    private String h5BaseResource;

    @Override
    public String toString() {
        return "B2CMallServiceConfig{" +
                "pageNotFoundSite='" + pageNotFoundSite + '\'' +
                ", cdnDomain='" + cdnDomain + '\'' +
                ", pcBaseResource='" + pcBaseResource + '\'' +
                ", h5BaseResource='" + h5BaseResource + '\'' +
                '}';
    }

    private B2CMallServiceConfig() {
    }

    private static final class B2CMallServiceConfigHolder {
        private static B2CMallServiceConfig INSTANCE = new B2CMallServiceConfig();

        static {
            InputStream in = null;
            try {
                in = B2CMallServiceConfigHolder.class.getResourceAsStream("/service-config.properties");
                Properties properties = new Properties();
                properties.load(in);
                // 页面找不到
                INSTANCE.setPageNotFoundSite(properties.getProperty(PAGE_NOT_FOUND, "/error.html"));
                //cdn domain
                INSTANCE.setCdnDomain(properties.getProperty(CDN_DOMAIN, Constants.DEFAULT_CMS_CDN_DOMAIN_VALUE));
                INSTANCE.setPcBaseResource(properties.getProperty(PC_BASE_RES, "http://imgcms.tepin.hk/youa/web-common-oss"));
                INSTANCE.setH5BaseResource(properties.getProperty(H5_BASE_RES, "http://imgcms.tepin.hk/youah5/h5-common-oss"));
            } catch (Exception e) {
                throw new RuntimeException("初始化B2CMallServiceConfig配置出错!", e);
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        throw new RuntimeException("failed close stream!", e);
                    }
                }
            }
            logger.info("B2CMallServiceConfig dump:{}", INSTANCE.toString());
        }
    }

    public static B2CMallServiceConfig getInstance() {
        return B2CMallServiceConfigHolder.INSTANCE;
    }

    public String getPageNotFoundSite() {
        return pageNotFoundSite;
    }

    private void setPageNotFoundSite(String pageNotFoundSite) {
        this.pageNotFoundSite = pageNotFoundSite;
        logger.info("[B2CMallServiceConfig.init]" + PAGE_NOT_FOUND + "{}", pageNotFoundSite);
    }

    public String getCdnDomain() {
        return cdnDomain;
    }

    public void setCdnDomain(String cdnDomain) {
        this.cdnDomain = cdnDomain;
        logger.info("[B2CMallServiceConfig.init]" + CDN_DOMAIN + ":{}", this.cdnDomain);
    }

    public String getPcBaseResource() {
        return pcBaseResource;
    }

    public void setPcBaseResource(String pcBaseResource) {
        this.pcBaseResource = pcBaseResource;
    }

    public String getH5BaseResource() {
        return h5BaseResource;
    }

    public void setH5BaseResource(String h5BaseResource) {
        this.h5BaseResource = h5BaseResource;
    }
}
